console.log('已注册项目', apps)

let namespace = 'SKYSTART'
let currentApp = ''

const setCache = (key, value) => {
  if (!window[namespace]) {
    window[namespace] = {}
  }
  window[namespace][key] = value
}

const getCache = (key) => {
  return window[namespace] && window[namespace][key] && window[namespace][key]
}

const fileType = (resources) => {
  return resources.split('.')[resources.split('.').length - 1]
}

const createJs = (resources, dataname) => {
  var script = document.createElement("script");
  script.src = resources;
  script.setAttribute('resources', dataname + 'resources')
  var head = document.getElementsByTagName("head")[0];
  head.appendChild(script);
}

const createCss = (resources, dataname) => {
  var style = document.createElement("link");
  style.href = resources
  style.setAttribute('resources', dataname + 'resources')
  style.rel = 'stylesheet'
  var head = document.getElementsByTagName("head")[0];
  head.appendChild(style);
}

const operation = {
  'css': createCss,
  'js': createJs,
}

/** */
apps.map((item => {
  Object.values(item.url).map((unit) => {
    namespace = item.type
    setCache('root', true)
    operation[fileType(unit)](unit, item.type)
  })
}))

// (1)、页面所有内容加载完成执行
window['appexecute'] = {}
window.onload = function () {
  apps.map((item) => {
    if (!window['appexecute'][item.type]) {
      window['appexecute'][item.type] = {}
    }
    //注册对应应用所有钩子
    namespace = item.type
    window['appexecute'][namespace]['appEnter'] = getCache('appEnter')
    window['appexecute'][namespace]['appLeave'] = getCache('appLeave')
  })

  window.location.hash = '#/' + apps[0] && apps[0]['type'] + '/'

}

function hashChangeFire () {
  console.log('hash改变了')
  let hash = window.location.hash
  let linkApp = hash.split('/')[1]
  if (!currentApp) {
    currentApp = apps[0] && apps[0]['type']
    window['appexecute'][currentApp]['appEnter']()
  } else if (currentApp != linkApp) {
    currentApp = linkApp
    window['appexecute'][linkApp]['appEnter']()
  } else {

  }
}

if (("onhashchange" in window) && ((typeof document.documentMode == "undefined") || document.documentMode8)) {
  // 浏览器支持onhashchange事件
  window.onhashchange = hashChangeFire; // TODO，对应新的hash执行的操作函数
} else {
  // 不支持则用定时器检测的办法
  setInterval(function () {
    var ischanged = isHashChanged(); // TODO，检测hash值或其中某一段是否更改的函数
    if (ischanged) {
      hashChangeFire(); // TODO，对应新的hash执行的操作函数
    }
  }, 150);
}
